import hashlib
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager
from django.db import models


# Create your models here.
class RedisUser(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=150, unique=True, db_index=True, verbose_name="用户账号",
                                help_text="用户账号")
    nickname = models.CharField(max_length=30, null=True, blank=True, verbose_name='昵称')
    first_name = models.CharField(verbose_name='first name', max_length=150, blank=True)
    last_name = models.CharField(verbose_name='last name', max_length=150, blank=True)
    is_staff = models.BooleanField(verbose_name='staff status', default=False)
    is_active = models.BooleanField(verbose_name='active', default=True)
    date_joined = models.DateTimeField(verbose_name='date joined', default=timezone.now)
    mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name='手机号码')
    email = models.EmailField(max_length=255, verbose_name="邮箱", null=True, blank=True, help_text="邮箱")
    # image = models.ImageField(upload_to='images/%Y/%m/%d/', default='images/default.png', max_length=100)
    login_status = (
        (0, '在线'),
        (1, '离线'),
        (2, '忙碌'),
    )
    login_status = models.SmallIntegerField(choices=login_status, default=0, verbose_name='登录状态')
    # # (1, "男"),(2, "女"),
    gender = models.IntegerField(default=1, verbose_name="性别", null=True, blank=True, help_text="性别")
    USER_TYPE = ((0, "后台用户"), (1, "前台用户"),)
    user_type = models.IntegerField(choices=USER_TYPE, default=0, verbose_name="用户类型", null=True, blank=True,
                                    help_text="用户类型")
    # is_superuser = models.BooleanField(default=False)

    objects = UserManager()
    USERNAME_FIELD = 'username'

    class Meta:
        db_table = 'redis_user'
        verbose_name = '用户表'
        verbose_name_plural = '用户表'

    # def get_login_status(self):
    #     return self.login_status

    # def set_password(self, raw_password):
    #     super().set_password(hashlib.md5(raw_password.encode(encoding="UTF-8")).hexdigest())

    # def save(self, *args, **kwargs):
    #     self.password = hashlib.md5(self.password.encode(encoding="UTF-8")).hexdigest()
    #     super().save(*args, **kwargs)
